<template>
	<view class="uni-container flex flex-col h-full">
		<div v-if="!isView">
			<u-button type="success" size="default" @click="scanCode">扫描二维码</u-button>
		</div>
		<div class="flex-auto overflow-auto">
			<u-form ref="uForm" :model="formData">
				<u-form-item label="分类">
					<u-input v-model="typeName" v-if="isView" disabled />
					<u-input v-model="typeName" @click="typeShow=true" v-if="!isView" disabled />
					<u-select v-model="typeShow" :list="typeList" @confirm="typeConfirm"></u-select>
				</u-form-item>
				<u-form-item label="库存">
					<u-input v-model="formData.stocks" :disabled="isView" />
				</u-form-item>
				<u-form-item label="封面" v-if="formData.photoUrl">
					<u-image height="200rpx" mode="heightFix" :src="formData.photoUrl"></u-image>
				</u-form-item>
				<u-form-item label="书名" prop="name">
					<u-input v-model="formData.name" :disabled="isView" />
				</u-form-item>
				<u-form-item label="作者">
					<u-input v-model="formData.author" :disabled="isView" />
				</u-form-item>
				<u-form-item label="价格">
					<u-input v-model="formData.price" :disabled="isView" />
				</u-form-item>
				<u-form-item label="简介">
					<u-input type="textarea" v-model="formData.description" :disabled="isView" />
				</u-form-item>
			</u-form>
		</div>
		<div v-if="!isView">
			<u-button type="success" size="default" @click="save">保存</u-button>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isView: false,
				typeShow: false,
				typeList: [],
				typeName:'请选择',
				formData: {
					stocks: 1
				},
				formOptions: {},
				rules: {
					name: [{
						required: true,
						message: '请输入书名',
						// 可以单个或者同时写两个触发验证方式
						trigger: 'blur,change'
					}]
				}
			}
		},
		onLoad(e) {
			
			this._id = e.id
			let act = e.act ? e.act : ""
			
			if (act == "view") {
				this.isView = true
			}
			this.getData(this._id)
		},
		onShow() {
			
			
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			
		},
		methods: {
			getData(id) {
				uni.showLoading({
					title: '加载中...'
				})
				if(id){
					this.$reqFun.req("books", {
						action: 'view',
						id: id
					}).then(res => {
						this.formData = res.result.data[0]
						this.getTypeList()
					}).finally(() => {
						uni.hideLoading()
					})
				}else{
					this.getTypeList()
					uni.hideLoading()
				}

			},
			getTypeList() {
				this.$reqFun.req("books", {
					action: 'typeList'
				}).then(res => {
					
					var list = res.result.data.map(o => {
						return {
							value: o._id,
							label: o.name
						}
					});
					this.typeList = list
					if(this.formData.type){
						this.typeName = this.typeInit(list,this.formData.type)
					}
				})
			},
			typeInit(list,id) {
				var obj = list.find(item => {
					return item.value === id
				})
				return obj.label
			},
			save() {
				let sendData = {
					action: "save",
					id: this._id,
					create_date: new Date().getTime(),
					type: this.formData.type,
					stocks: this.formData.stocks,
					name: this.formData.name,
					author: this.formData.author,
					code: this.formData.code,
					douban: this.formData.douban,
					photoUrl: this.formData.photoUrl,
					price: this.formData.price,
					description: this.formData.description
				}
				this.btnLoading = true
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$reqFun.req("books", sendData).then(res => {
							this.btnLoading = false
							uni.reLaunch({
								url: './list'
							})
						})
					} else {
						console.log('验证失败');
					}
				});

			},
			typeConfirm(e) {
				this.typeName = e[0].label
				this.formData.type = e[0].value
			},
			scanCode() {
				let _that = this;
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						_that.getBook(res.result)
					}
				});
			},
			getBook(id) {
				uni.showLoading({
					mask: true
				})
				uni.request({
					url: 'https://api.zuk.pw/situ/book/isbn/' + id, //仅为示例，并非真实接口地址。
					success: (res) => {
						// this.formData = Object.assign(this.formData,res.data.data)
						this.formData = {
							...this.formData,
							...res.data.data
						}
						uni.hideLoading()
					},
					fail: (res) => {
						this.$refs.uToast.show({
							title: '获取书籍失败',
							type: 'error',
							url: '/pages/books/list'
						})
					}
				});
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
		padding: 12px 20px;
		font-size: 14px;
		border-radius: 4px;
		line-height: 1;
		margin: 0;
	}
</style>
